package com.dszq.behavior.service.impl;

import com.dszq.behavior.service.ApUnlikesBehaviorService;
import com.dszq.common.constants.BehaviorConstants;
import com.dszq.common.redis.CacheService;
import com.dszq.model.behavior.dtos.UnLikesBehaviorDto;
import com.dszq.model.common.dtos.ResponseResult;
import com.dszq.model.common.enums.AppHttpCodeEnum;
import com.dszq.model.user.pojos.ApUser;
import com.dszq.utils.thread.AppThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * [一句话描述该类的功能]
 *
 * @author : [Lenovo]
 * @version : [v1.0]
 * @createTime : [2024/5/2 16:39]
 */
@Service
@Slf4j
public class ApUnlikesBehaviorServiceImpl implements ApUnlikesBehaviorService {
    @Autowired
    private CacheService cacheService;
    /**
     * 不喜欢
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult unLike(UnLikesBehaviorDto dto) {
        //1.检查参数
        if (dto == null || dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.是否登录
        ApUser user = AppThreadLocalUtil.getUser();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        if (dto.getType()==0)
        {
            Object object = cacheService.hGet(BehaviorConstants.UN_LIKE_BEHAVIOR + dto.getArticleId(), user.getId().toString());
            if (object!=null)
            {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"已设置不喜欢，请勿重复操作");
            }
            //不喜欢
            log.info("保存当前key:{} ,{}, {}", dto.getArticleId(), user.getId(), dto);
            cacheService.hPut(BehaviorConstants.UN_LIKE_BEHAVIOR+dto.getArticleId(),user.getId().toString(),dto.toString());
        }else {
            //取消不喜欢
            log.info("保存当前key:{} ,{}, {}", dto.getArticleId(), user.getId(), dto);
            cacheService.hDelete(BehaviorConstants.UN_LIKE_BEHAVIOR+dto.getArticleId(),user.getId().toString());
        }
        return null;
    }
}
